package com.niit.onlinemusicserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.niit.onlinemusicserver.common.R;
import com.niit.onlinemusicserver.mapper.SingerMapper;
import com.niit.onlinemusicserver.model.domain.Singer;
import com.niit.onlinemusicserver.model.request.SingerRequest;
import com.niit.onlinemusicserver.service.SingerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {

    @Autowired
    private SingerMapper singerMapper;
    @Override
    public R addSinger(SingerRequest addSingerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(addSingerRequest,singer);
        String pic = "img/avatorImages/user.jpg";
        singer.setPic(pic);
        if(singerMapper.insert(singer)>0){
            return R.success("添加成功");
        }else{
            return R.error("添加失败");
        }
    }

    @Override
    public R updateSingerMsg(SingerRequest updateSingerRequest) {
        Singer singer = new Singer();
        BeanUtils.copyProperties(updateSingerRequest,singer);
        String pic = "img/avatorImages/user.jpg";
        singer.setPic(pic);
        if(singerMapper.updateById(singer)>0){
            return R.success("修改成功");
        }else{
            return R.error("修改失败");
        }
    }

    @Override
    public R updateSingerPic(MultipartFile avatorFile, int id) {
        String fileName = System.currentTimeMillis() + "-" + avatorFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img"
                + System.getProperty("file.separator") + "singerPic";
        File file1 = new File(filePath);
        if (!file1.exists()) {
            file1.mkdir();
        }

        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String imgPath = "/img/singerPic/" + fileName;
        //String imgPath = "img/singerPic";

        try {
            avatorFile.transferTo(dest);
        } catch (IOException e) {
            return R.fatal("上传失败" + e.getMessage());
        }

        /*
        // 使用阿里云对象存储服务替换原来的存储在本地的流程
        String ossFilePath;
        try {
            ossFilePath = oss.uploadFile(imgPath, fileName, avatorFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        */
        Singer singer = new Singer();
        singer.setId(id);
        singer.setPic(imgPath);
        if (singerMapper.updateById(singer) > 0) {
            return R.success("上传成功", imgPath);
        } else {
            return R.error("上传失败");
        }
    }


    @Override
    public R deleteSinger(Integer id) {
       if(singerMapper.deleteById(id)>0){
           return R.success("删除成功");
       }else{
           return R.error("删除失败");
       }
    }

    @Override
    public R allSinger() {
      List<Singer> list = singerMapper.selectList(null);
      return R.success("操作成功",list);
    }

    @Override
    public R singerOfName(String name) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Singer> list = singerMapper.selectList(queryWrapper);
        return R.success("操作成功",list);
    }

    @Override
    public R singerOfSex(Integer sex) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sex",sex);
        List<Singer> list = singerMapper.selectList(queryWrapper);
        return R.success("操作成功",list);
    }
}
